//
//  ScopePayViewController.m
//  JxCarios
//
//  Created by xcq on 15/2/28.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "ScopePayViewController.h"
#import "payTypeSeleteTableViewCell.h"
#import "ScopePayTableViewCell.h"
#import "ConfigDefine.h"

static NSString *payType = @"payTypeSelete";
static NSString *scope = @"scopePay";
@interface ScopePayViewController () <UITableViewDataSource,pushMessegeToSuper,UITableViewDelegate>{
  NSMutableArray *checkCellArr;

}
@property (retain, nonatomic)  Product *pro;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *payBtn;
//可用积分
@property (strong, nonatomic) NSString *canScope;
//支付方式数组
@property (copy , nonatomic) NSArray *tableDataSource;

@end

@implementation ScopePayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initAbout];
    [self netRequest];
}


- (void)netRequest {
    if (!_carAround) {
        _carAround = [CarAroundDataSource dataSourceWithRequestor:self];
    }
    [_carAround getScopePayRule:^(id result) {
        _canScope = [result customObjectForkey:@"vb"];
        _tableDataSource = [NSArray arrayWithArray:[result customObjectForkey:@"pay_way"]];
        [self initDataSource];
    } anfFail:^(id result) {
        
    }];
}

- (void)initDataSource {
    if (_tableDataSource.count <= 0) {
        return;
    }
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView reloadData];
    
}

- (void)initAbout {
    self.title = @"支付";
    [self showNavBarWithRightTitle:nil andRightAction:nil andLeftAction:@selector(backTo) andCustom:NO];
    [self.tableView registerNib:[UINib nibWithNibName:@"payTypeSeleteTableViewCell" bundle:nil] forCellReuseIdentifier:payType];
    [self.tableView registerNib:[UINib nibWithNibName:@"ScopePayTableViewCell" bundle:nil] forCellReuseIdentifier:scope];
    checkCellArr = [NSMutableArray new];
}

#pragma mark - tableView DataSource & delegate 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ScopePayTableViewCell *payCell = [tableView dequeueReusableCellWithIdentifier:scope forIndexPath:indexPath];
        payCell.info = [NSDictionary dictionaryWithObjectsAndKeys:_payPrice,@"payPrice",_payScope,@"payScope", nil];
         return payCell;
    }else{
        payTypeSeleteTableViewCell *selete = [tableView dequeueReusableCellWithIdentifier:payType forIndexPath:indexPath];
        selete.selectionStyle = UITableViewCellSelectionStyleNone;
        selete.pushDelegate = self;
        selete.notSelete = YES;
        selete.seleteTypeImg.image = [UIImage imageNamed:@"zhifu-01"];
        selete.seleteTypeName.text = @"支付宝";
        selete.checkBtn.selected = YES;
        selete.seleteTypeDetail.text = @"推荐开通支账户用户使用,积分奖励";
          
        if (checkCellArr.count < _tableDataSource.count) {
            [checkCellArr addObject:selete];
        }
        return selete;
       
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 30)];
    view.backgroundColor = self.tableView.backgroundColor;
    if (section == 1) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 80, 20)];
        label.font = [UIFont systemFontOfSize:13.0f];
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentLeft;
        label.text = @"选择支付方式";
        [view addSubview:label];
        return view;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 30;
    }
    return 0;
}

- (void)pushSelfToView:(payTypeSeleteTableViewCell *)cell andType:(BOOL)bol andSelete:(BOOL)selete{
    payTypeSeleteTableViewCell *temp = nil;
    if (checkCellArr.count == 0) {
        return;
    }
    if (cell == (payTypeSeleteTableViewCell *)[checkCellArr firstObject]) {
        temp = (payTypeSeleteTableViewCell *)[checkCellArr lastObject];
    }else{
        temp = [checkCellArr firstObject];
    }
    temp.checkBtn.selected = NO;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 80;
    }else{
        return 60;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (IBAction)payAction:(id)sender {
    if (_payPrice.length == 0) {
        return;
    }
    if(!_pro){
        _pro = [Product new];
    }
     _carAround.priceSum = [_payPrice substringFromIndex:1];
       [_carAround userVbPay:^(id result) {
           _pro.orderId = [result customObjectForkey:@"order_id"];
           _pro.subject = @"购买积分";
           _pro.body = @"商品列表";
           _pro.price = [[result customObjectForkey:@"topup_money"] doubleValue];
           [self CreateOrderInfo:_pro
                         tradeNo:[result customObjectForkey:@"order_id"]
                        callback:^(NSDictionary *resultDic) {
                            if ( [[resultDic objectForKey:@"status"] isEqualToString:@"9000"])
                            {
                                [self.navigationController.view makeToast:@"支付成功"];
                                
                            }else if ([[resultDic objectForKey:@"status"] isEqualToString:@"4000"])
                            {
                                [self.view makeToast:@"订单支付失败" duration:2.0f position:TOP_POINT];
                                
                            }else if ([[resultDic objectForKey:@"status"] isEqualToString:@"6001"]){
                                [self.view makeToast:@"您中途取消了支付" duration:2.0f position:TOP_POINT];
                                
                            }else if ([[resultDic objectForKey:@"status"] isEqualToString:@"6002"])
                            {
                                
                                [self.view makeToast:@"网络不通畅,请重试" duration:2.0f position:TOP_POINT];
                                
                            }
                        }];

        
    } anfFail:^(id result) {
        
    }];
}
@end
